package com.syqc.monitor.ui.cost

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.core.view.isVisible
import com.syqc.monitor.R
import com.syqc.monitor.databinding.PopupUploadBinding

class UploadPopup(context: Context, private val onClick: (Boolean) -> Unit) : PopupWindow() {
    private val binding = PopupUploadBinding.inflate(LayoutInflater.from(context))

    init {
        contentView = binding.root
        isClippingEnabled = false
        isFocusable = true //popupwindow设置焦点
        setBackgroundDrawable(ColorDrawable(-0x56000000)) //设置背景
//        isOutsideTouchable = true //点击外面窗口消失
        width = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的宽
        height = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的高
        initEvent()
    }


    fun initEvent() {
        binding.tvUpload.setOnClickListener {
            onClick.invoke(false)
            dismiss()
        }
        binding.tvBack.setOnClickListener {
            dismiss()
        }
        binding.tvNew.setOnClickListener {
            onClick.invoke(true)
            dismiss()
        }
    }

    fun show(view: View, isSuccess: Boolean) {
        binding.tvUpload.isVisible = !isSuccess
        binding.tvNew.isVisible = isSuccess
        binding.tvToast.isSelected = !isSuccess
        binding.tvToast.setText(
            if (isSuccess) {
                R.string.desc_upload_success
            } else {
                R.string.desc_upload_fail
            }
        )
        showAtLocation(view, Gravity.NO_GRAVITY, 0, 0)
    }
}